﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using VM.Common.DataUntil;
using VM.Common.Encryption5Until;
using VM.Common.Request;
using VM.Entity.EntityDto.WX;
using VM.IBLL.Goods;
using VM.IBLL.System.Code;
using VM.WebCore.Jwt;
using VM.WebCore.SwaggerExtend;
using VM.Wechat;
using HttpClientRequest = VM.Common.Request.HttpClientRequest;

namespace VM.WebAPI.Controllers
{
    [Route("api/wx")]
    [ApiController]
    public class WXValidController : ControllerBase
    {
        private readonly IJWTService _IJWTService;


        public WXValidController(IJWTService iJWTService)
        {

            
            _IJWTService = iJWTService;
             
        }


        /// <summary>
        /// 微信小程序获取OpenId
        /// </summary>
        /// <param name="js_code"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("getOpenValid")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public string getOpenValid(string js_code)
        {
            string Url = $"https://api.weixin.qq.com/sns/jscode2session?appid=wx0d811fefbcb62d4b&secret=e5579d7c863222c2de9e73b8966a0461&js_code=" + $@"{js_code}" + "&grant_type=authorization_code";
         
            return HttpClientRequest.GetFunction(Url);

        }

        /// <summary>
        /// 统一微信下单接口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过] PC
        [Route("addWXOrder")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public string addWXOrder(addWXOrderDto obj)
        {
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();
            string orderNo= UserId.ToString() + RandomNo.GetRandomNo6().ToString() + Timestamp.getNowTimeStamp().ToString();

            string Res=  wxHelper.unifiedorder(obj.openId, orderNo, obj.amount);

            return Res;

        }



    }
}
